//
//  CLCoveryManager.m
//  chanlin
//
//  Created by Ryan on 14-12-23.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLCoveryManager.h"
#import "IFCoreDataManager.h"
#import "CLCovery.h"
#import "SgrGCD.h"
#import "CLPushWindow.h"
#import "CLCoveryController.h"

@implementation CLCoveryManager

SGR_DEF_SINGLETION(CLCoveryManager)

- (void)showCovery{
   NSString *file=[[self  k_cacheUrl] stringByAppendingPathComponent:@"imgfile"];
   
            UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfFile:file]];
            if(img){
                NSArray *array=[CLCovery fetchAllWithMoc:[IFCoreDataManager sharedInstance].mainMoc];
                CLCovery *covery=(CLCovery *)[array firstObject];
                CLPushWindow *window=[[CLPushWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                window.windowLevel=UIWindowLevelAlert;
                [CLGloble sharedInstance].pushWindow=window;
                CLCoveryController *ctrl=[[CLCoveryController alloc] init];
                ctrl.coverImg=img;
                if(covery){
                    ctrl.titleStr=covery.title;
                }
                window.rootViewController=ctrl;
                [window makeKeyAndVisible];
            }
  
}

- (void)updateCovery{
    NSString *url=[NSString stringWithFormat:@"http://appv2.chanlin.org/e/app/cover.php?size=%f",GlobleHeight*2];
    [self getWithUrl:url
               param:nil
           dataBlock:^(BOOL success, id responseObj, NSError *message, NSMutableDictionary *param) {
               if(success && [responseObj isKindOfClass:[NSDictionary class]]){
                   NSString *imgurl=[[[responseObj sgrGetArrayForKey:@"list"]
                                     sgrGetDictionaryForIndex:0] sgrGetStringForKey:@"titlepic"];
                   NSString *title=[[[responseObj sgrGetArrayForKey:@"list"]
                                      sgrGetDictionaryForIndex:0] sgrGetStringForKey:@"title"];
                   
                   [[IFCoreDataManager sharedInstance] performBlock:^(NSManagedObjectContext *moc) {
                       NSArray *array=[CLCovery fetchAllWithMoc:moc];
                       for(CLCovery *covery in array){
                           [moc deleteObject:covery];
                       }
                       CLCovery *covery=[CLCovery managerObjWithMoc:moc];
                       covery.imgUrl=imgurl;
                       covery.title=title;
                       [[IFCoreDataManager sharedInstance] saveContext:moc ToPersistentStore:nil];
                       
                   }];
                   
                   [param sgrSetObject:imgurl forKey:@"imgUrl"];
               }
           }
             uiBlock:^(BOOL success, id responseObj, NSError *message, NSMutableDictionary *param) {
                 NSString *url=[param sgrGetStringForKey:@"imgUrl"];
                 if(_isStrNotNull(url)){
                     [[SgrGCD sharedInstance] enqueueGloble:^{
                         NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
                         UIImage *img=[UIImage imageWithData:data];
                         if(img){
                             NSString *file=[[self  k_cacheUrl] stringByAppendingPathComponent:@"imgfile"];
                             if([[NSFileManager defaultManager] fileExistsAtPath:file]){
                                 [[NSFileManager defaultManager] removeItemAtPath:file error:nil];
                             }
                             [data writeToFile:file atomically:YES];
                         }
                     }];
                 }
             }];
}

@end
